package org.light.simpleauth.verb;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.TreeSet;

import org.light.complexverb.Assign;
import org.light.core.Verb;
import org.light.core.Writeable;
import org.light.domain.Domain;
import org.light.domain.Dropdown;
import org.light.domain.Field;
import org.light.domain.FieldSerialComparator;
import org.light.domain.JavascriptBlock;
import org.light.domain.JavascriptMethod;
import org.light.domain.Method;
import org.light.domain.Signature;
import org.light.domain.Statement;
import org.light.domain.StatementList;
import org.light.domain.Type;
import org.light.domain.Var;
import org.light.easyui.EasyUIPositions;
import org.light.exception.ValidateException;
import org.light.generator.NamedStatementGenerator;
import org.light.generator.NamedStatementListGenerator;
import org.light.utils.FieldUtil;
import org.light.utils.InterVarUtil;
import org.light.utils.StringUtil;
import org.light.utils.WriteableUtil;
import org.light.verb.FindByName;

public class AddUser extends Verb implements EasyUIPositions {
	protected Domain roleDomain;

	public Domain getRoleDomain() {
		return roleDomain;
	}

	public void setRoleDomain(Domain roleDomain) {
		this.roleDomain = roleDomain;
	}

	@Override
	public Method generateDaoImplMethod() throws Exception {
		if (this.denied) return null;
		else {
			Method method = new Method();
			method.setStandardName("add" + StringUtil.capFirst(this.domain.getStandardName()));
			method.setReturnType(new Type("boolean"));
			method.setThrowException(true);
			method.addAdditionalImport("java.sql.Connection");
			method.addAdditionalImport("java.util.List");
			method.addAdditionalImport("java.util.ArrayList");
			method.addAdditionalImport("java.sql.PreparedStatement");
			method.addAdditionalImport("java.sql.ResultSet");
			method.addAdditionalImport(this.domain.getPackageToken()+"."+this.domain.getDomainSuffix()+"."+this.domain.getCapFirstDomainNameWithSuffix());
			method.addAdditionalImport(this.domain.getPackageToken()+"."+this.domain.getDaoSuffix()+"."+this.domain.getStandardName()+"Dao");
			method.addSignature(new Signature(1,InterVarUtil.DB.connection.getVarName(),InterVarUtil.DB.connection.getVarType()));
			method.addSignature(new Signature(2, StringUtil.lowerFirst(this.domain.getStandardName()), this.domain.getType()));
			
			List<Writeable> sList = new ArrayList<Writeable>();
			sList.add(NamedStatementGenerator.getAddSqlStatement(1000L,2, this.domain,InterVarUtil.DB.query));
			sList.add(NamedStatementGenerator.getPrepareStatementWithReturnGeneratedKeys(2000L,2, InterVarUtil.DB.ps, InterVarUtil.DB.query, InterVarUtil.DB.connection));
			sList.add(NamedStatementListGenerator.generatePsSetDomainFieldsWithoutId(3000L, 2,this.domain, InterVarUtil.DB.ps));
			sList.add(NamedStatementGenerator.getPrepareStatementExcuteUpdate(4000L,2, InterVarUtil.DB.result, InterVarUtil.DB.ps));
			sList.add(NamedStatementListGenerator.generateResultReturnSuccessUpdatedDomainId(5000L,2, InterVarUtil.DB.result,InterVarUtil.DB.ps,this.domain));
		
			method.setMethodStatementList(WriteableUtil.merge(sList));		
	
			return method;
		}
	}
		
	@Override
	public Method generateDaoMethodDefinition() throws Exception {
		if (this.denied) return null;
		else {
			Method method = new Method();
			method.setStandardName("add"+StringUtil.capFirst(this.domain.getStandardName()));
			method.setReturnType(new Type("boolean"));
			method.setThrowException(true);
			method.addAdditionalImport("java.util.List");
			method.addAdditionalImport(this.domain.getPackageToken()+ "."+this.domain.getDomainSuffix()+"."+this.domain.getCapFirstDomainNameWithSuffix());
			method.addSignature(new Signature(1,InterVarUtil.DB.connection.getVarName(),InterVarUtil.DB.connection.getVarType()));
			method.addSignature(new Signature(2, StringUtil.lowerFirst(this.domain.getStandardName()), this.domain.getType()));
			return method;
		}
	}

	@Override
	public Method generateServiceMethodDefinition() throws Exception {
		if (this.denied) return null;
		else {
			Method method = new Method();
			method.setStandardName("add"+StringUtil.capFirst(this.domain.getStandardName()));
			method.setReturnType(new Type("boolean"));
			method.setThrowException(true);
			method.addAdditionalImport("java.util.List");
			method.addAdditionalImport(this.domain.getPackageToken()+ "."+this.domain.getDomainSuffix()+"."+this.domain.getCapFirstDomainNameWithSuffix());
			method.addSignature(new Signature(1, StringUtil.lowerFirst(this.domain.getStandardName()), this.domain.getType()));
			return method;
		}
	}

	@Override
	public Method generateServiceImplMethod() throws Exception  {
		if (this.denied) return null;
		else {
			Method method = new Method();
			method.setStandardName("add" + StringUtil.capFirst(this.domain.getStandardName()));
			method.addSignature(new Signature(1, this.domain.getLowerFirstDomainName(), new Type(this.domain.getCapFirstDomainNameWithSuffix())));
			method.setReturnType(new Type("boolean"));
			method.setThrowException(true);
			method.addAdditionalImport(this.domain.getPackageToken()+".utils.PasswordUtil");

			Method daomethod = this.generateDaoMethodDefinition();
			List<Writeable> sList = new ArrayList<Writeable>();
			sList.add(NamedStatementGenerator.getDBConfInitDBAutoRelease(1000L, 2, InterVarUtil.DB.connection, InterVarUtil.DB.dbconf));
			sList.add(new Statement(1000L,3,"String "+this.domain.findFieldByFixedName("salt").getLowerFirstFieldName()+" = PasswordUtil.generateRandomSalt();"));
			sList.add(new Statement(2000L,3,"String _encPassword = PasswordUtil.multiSha1("+this.domain.getLowerFirstDomainName()+".get"+this.domain.findFieldByFixedName("password").getCapFirstFieldName()+"(), "+this.domain.findFieldByFixedName("salt").getLowerFirstFieldName()+", 3);"));
			sList.add(new Statement(3000L,3,this.domain.getLowerFirstDomainName()+".set"+this.domain.findFieldByFixedName("salt").getCapFirstFieldName()+"("+this.domain.findFieldByFixedName("salt").getLowerFirstFieldName()+");"));
			sList.add(new Statement(4000L,3,"user.set"+this.domain.findFieldByFixedName("password").getCapFirstFieldName()+"(_encPassword);"));
			sList.add(new Statement(5000L,3,"user.set"+this.domain.findFieldByFixedName("loginFailure").getCapFirstFieldName()+"(0);"));
			sList.add(new Statement(6000L,3,"return "+daomethod.generateStandardDaoImplCallString(InterVarUtil.DB.dao.getVarName())+";"));
			sList.add(NamedStatementGenerator.getLoopFooter(7000L, 2));
			method.setMethodStatementList(WriteableUtil.merge(sList));
			return method;
		}
	}
	
	public AddUser(Domain domain,Domain roleDomain) throws ValidateException{
		super();
		this.domain = domain;
		this.roleDomain = roleDomain;
		this.setVerbName("Add"+StringUtil.capFirst(this.domain.getStandardName()));
		this.setLabel("新增");
		if  (domain.getLanguage().equalsIgnoreCase("english"))  this.setLabel("Add");
	}

	@Override
	public Method generateControllerMethod() throws Exception {
		Method method = new Method();
		method.setStandardName("processRequest");
		method.setReturnType(new Type("void"));
		method.setThrowException(true);
		List<String> list = new ArrayList<String>();
		list.add("ServletException");
		list.add("IOException");
		method.setIsprotected(true);
		method.setOtherExceptions(list);
		method.addSignature(new Signature(1,"request",new Type("HttpServletRequest","javax.servlet.http")));
		method.addSignature(new Signature(2,"response",new Type("HttpServletResponse","javax.servlet.http")));
		method.addAdditionalImport("java.io.IOException");
		method.addAdditionalImport("java.io.PrintWriter");
		method.addAdditionalImport("javax.servlet.ServletException");
		method.addAdditionalImport("javax.servlet.http.HttpServlet");
		method.addAdditionalImport("javax.servlet.http.HttpServletRequest");
		method.addAdditionalImport("javax.servlet.http.HttpServletResponse");
		method.addAdditionalImport("java.util.Map");
		method.addAdditionalImport("java.util.TreeMap");
		method.addAdditionalImport("net.sf.json.JSONObject");
		method.addAdditionalImport("java.io.BufferedReader");
		method.addAdditionalImport(this.domain.getPackageToken()+".utils.StringUtil");
		method.addAdditionalImport(this.domain.getPackageToken()+"."+this.domain.getDomainSuffix()+"."+this.domain.getCapFirstDomainNameWithSuffix());
		method.addAdditionalImport(this.domain.getPackageToken()+"."+this.domain.getServiceSuffix()+"."+this.domain.getStandardName()+"Service");
		method.addAdditionalImport(this.domain.getPackageToken()+"."+this.domain.getServiceimplSuffix()+"."+this.domain.getStandardName()+"ServiceImpl");
		method.addAdditionalImport(this.roleDomain.getPackageToken()+"."+this.roleDomain.getDomainSuffix()+"."+this.roleDomain.getCapFirstDomainNameWithSuffix());
		method.addAdditionalImport(this.roleDomain.getPackageToken()+"."+this.roleDomain.getServiceSuffix()+"."+this.roleDomain.getStandardName()+"Service");
		method.addAdditionalImport(this.roleDomain.getPackageToken()+"."+this.roleDomain.getServiceimplSuffix()+"."+this.roleDomain.getStandardName()+"ServiceImpl");
		
		FindByName findUser = new FindByName(this.domain);
		FindByName findRole = new FindByName(this.roleDomain);
		Assign assignRole = new Assign(this.domain,this.roleDomain);
		
		List<Writeable> sList = new ArrayList<Writeable>();
		Var service = new Var("service", new Type(this.domain.getStandardName()+"Service",this.domain.getPackageToken()));
		Var resultMap = new Var("result", new Type("TreeMap<String,Object>","java.util"));
		sList.add(NamedStatementGenerator.getFacadeSetContentType(1000L, 2, InterVarUtil.Servlet.response, InterVarUtil.SimpleJEE.UTF8.getVarName()));
		sList.add(NamedStatementGenerator.getControllerPrintWriterOut(2000L, 2, InterVarUtil.Servlet.response, InterVarUtil.Servlet.out));
		sList.add(NamedStatementGenerator.getJsonResultMap(2500L, 2, resultMap));
		sList.add(NamedStatementGenerator.getTryHead(3000L, 2));
		sList.add(new Statement(4000L,3,"StringBuilder jsonBuilder = new StringBuilder();"));
		sList.add(new Statement(5000L,3,"BufferedReader reader = request.getReader();"));
		sList.add(new Statement(6000L,3,"String line;"));
		sList.add(new Statement(7000L,3,"while ((line = reader.readLine()) != null) {"));
		sList.add(new Statement(8000L,4,"jsonBuilder.append(line);"));
		sList.add(new Statement(9000L,3,"}"));
		sList.add(new Statement(10000L,3,"String jsonString = jsonBuilder.toString();"));
		sList.add(new Statement(11000L,3,"JSONObject jsonReq = JSONObject.fromObject(jsonString);"));
		sList.add(new Statement(12000L,0,""));
				
		sList.add(NamedStatementGenerator.getPrepareDomainVarInit(13000L, 3, this.domain));
		Set<Field> deniedFields = new TreeSet<>();
		deniedFields.add(this.domain.findFieldByFixedName("salt"));
		deniedFields.add(this.domain.findFieldByFixedName("loginFailure"));

		long serial = 14000L;
		for (Field f : domain.getFieldsWithoutImageAndId()){
			if (! deniedFields.contains(f)){
				sList.add(new Statement(serial,3,"if (!StringUtil.isBlank(jsonReq.getString(\""+f.getLowerFirstFieldName()+"\"))) {"));
				sList.add(new Statement(serial+1000L,4,this.domain.getLowerFirstDomainName()+".set"+f.getCapFirstFieldName()+"("+FieldUtil.generateRequestGetJsonParameterStringFromStringType(f)+");"));
				sList.add(new Statement(serial+2000L,3,"}"));
				if (!FieldUtil.findTypeDefaultValueString(f.getFieldType()).equals("null")){
					sList.add(new Statement(serial+3000L,3,"else {"));
					sList.add(new Statement(serial+4000L,4,this.domain.getLowerFirstDomainName()+".set"+f.getCapFirstFieldName()+"("+FieldUtil.findTypeDefaultValueString(f.getFieldType())+");"));
					sList.add(new Statement(serial+5000L,3,"}"));
				}
				serial += 6000L;
			}
		}
		sList.add(NamedStatementGenerator.getPrepareService(serial+1000L,3, service));
		sList.add(new Statement(serial+2000L,3,this.roleDomain.getCapFirstDomainName()+"Service "+this.roleDomain.getLowerFirstDomainName()+"Service = new "+this.roleDomain.getCapFirstDomainName()+"ServiceImpl();"));
		
		serial += 3000L;
		boolean imageExists = false;
		for (Field f : this.domain.getPlainFields()) {
			if (f.getFieldType().equalsIgnoreCase("image")) {
				imageExists = true;
				sList.add(new Statement(serial, 3,
						"if (" + this.domain.getCapFirstDomainName()+"ServiceImpl.temp" + this.domain.getCapFirstDomainName() + "!=null && " + this.domain.getCapFirstDomainName()+"ServiceImpl.temp"
								+ this.domain.getCapFirstDomainName() + ".get" + f.getCapFirstFieldName()
								+ "()!=null) {"));
				sList.add(new Statement(serial + 1000L, 4,
						this.domain.getLowerFirstDomainName() + ".set" + f.getCapFirstFieldName() + "(" + this.domain.getCapFirstDomainName()+"ServiceImpl.temp"
								+ this.domain.getCapFirstDomainName() + ".get" + f.getCapFirstFieldName()
								+ "());"));
				sList.add(new Statement(serial + 2000L, 3, "}"));
				serial += 3000L;
			}
		}
		if (imageExists) {
			sList.add(new Statement(serial + 1000L, 3, this.domain.getCapFirstDomainName()+"ServiceImpl.temp" + this.domain.getCapFirstDomainName() + " = new  "
					+ this.domain.getCapFirstDomainNameWithSuffix() + "();"));
			serial += 2000L;
		}

		sList.add(NamedStatementGenerator.getCallServiceMethodAssignSuccess(serial + 1000L, 3, service, generateServiceMethodDefinition()));
		sList.add(NamedStatementListGenerator.getPutJsonResultMapWithSuccessAndDomain(serial + 2000L, 3, resultMap,this.domain));
		
		sList.add(new Statement(serial+2100L,3,"if (success) {"));
		sList.add(new Statement(serial+2200L,4,this.roleDomain.getCapFirstDomainNameWithSuffix()+" _userRole = "+this.roleDomain.getLowerFirstDomainName()+"Service."+StringUtil.lowerFirst(findRole.getVerbName())+"(\"user\");"));
		sList.add(new Statement(serial+2300L,4,"service."+StringUtil.lowerFirst(assignRole.getVerbName())+"(user.get"+this.domain.getDomainId().getCapFirstFieldName()+"(),_userRole.get"+this.roleDomain.getDomainId().getCapFirstFieldName()+"().toString());"));
		sList.add(new Statement(serial+2400L,3,"}"));

		sList.add(NamedStatementGenerator.getEncodeMapToJsonResultMap(serial + 3000L, 3, resultMap,InterVarUtil.Servlet.out));
		sList.add(NamedStatementListGenerator.generateCatchExceptionPrintStackPrintJsonMapFinallyCloseOutFooter(serial + 4000L, 2, InterVarUtil.Servlet.response, resultMap, InterVarUtil.Servlet.out));
	
		method.setMethodStatementList(WriteableUtil.merge(sList));
		return method;
	}
	
	@Override
	public JavascriptBlock generateEasyUIJSButtonBlock() throws Exception {
		if (this.denied) return null;
		else {
			JavascriptBlock block = new JavascriptBlock();
			block.setSerial(100);
			block.setStandardName("add"+domain.getCapFirstDomainName());
			StatementList sl = new StatementList();
			sl.add(new Statement(1000,0, "{"));
			if (this.domain.getLanguage().equalsIgnoreCase("english")) {
				sl.add(new Statement(2000, 1, "text:'Add',"));
			}else {
				sl.add(new Statement(2000,1, "text:'新增',"));
			}
			sl.add(new Statement(3000,1, "iconCls:'icon-add',"));
			sl.add(new Statement(4000,1, "handler:function(){"));
			sl.add(new Statement(5000,2, "$('#wadd"+this.domain.getCapFirstDomainName()+"').window('open');"));
			sl.add(new Statement(6000,1, "}"));
			sl.add(new Statement(7000,0, "}"));
			block.setMethodStatementList(sl);
			return block;			
		}
	}
	@Override
	public JavascriptMethod generateEasyUIJSActionMethod() throws Exception {
		if (this.denied) return null;
		else {
			Domain domain = this.domain;
			JavascriptMethod method = new JavascriptMethod();
			method.setSerial(200);
			method.setStandardName("add"+domain.getCapFirstDomainName());
			
			StatementList sl = new StatementList();
			
			sl.add(new Statement(1000L,1,"if ($(\"#ff\").find(\"#"+this.domain.findFieldByFixedName("password").getLowerFirstFieldName()+"\").val()!=$(\"#ff\").find(\"#confirm"+this.domain.findFieldByFixedName("password").getCapFirstFieldName()+"\").val()){"));
			if ("english".equalsIgnoreCase(this.domain.getLanguage())) {
				sl.add(new Statement(2000L,2,"$.messager.alert(\"Error\",\"Password did not match.\",\"error\");"));
			}else {
				sl.add(new Statement(2000L,2,"$.messager.alert(\"错误\",\"密码不匹配！\",\"error\");"));
			}
			sl.add(new Statement(3000L,2,"return;"));
			sl.add(new Statement(4000L,1,"}"));
			
			sl.add(new Statement(5000,1, "if ($(\"#ff\").form(\"validate\")) {"));
			sl.add(new Statement(6000,2, "$.ajax({"));
			sl.add(new Statement(7000,3, "type: \"post\","));
			sl.add(new Statement(8000,3, "url: \"../"+this.domain.getControllerPackagePrefix()+domain.getLowerFirstDomainName()+domain.getControllerNamingSuffix()+"/add"+domain.getCapFirstDomainName()+"\","));
			sl.add(new Statement(9000,3, "data: JSON.stringify({"));
			
			sl.add(new Statement(10000,4, this.domain.findFieldByFixedName("userName").getLowerFirstFieldName()+":$(\"#ff\").find(\"#"+this.domain.findFieldByFixedName("userName").getLowerFirstFieldName()+"\").textbox(\"getValue\"),"));
			sl.add(new Statement(11000,4, this.domain.findFieldByFixedName("password").getLowerFirstFieldName()+":hex_sha1($(\"#ff\").find(\"#"+this.domain.findFieldByFixedName("password").getLowerFirstFieldName()+"\").textbox(\"getValue\")),"));
			
			Set<Field> normalFields = new TreeSet<>(new FieldSerialComparator());
			normalFields.addAll(domain.getFieldsWithoutId());
			Set<Field> deniedFields = new TreeSet<>(new FieldSerialComparator());
			deniedFields.add(this.domain.findFieldByFixedName("userName"));
			deniedFields.add(this.domain.findFieldByFixedName("password"));
			deniedFields.add(this.domain.findFieldByFixedName("salt"));
			deniedFields.add(this.domain.findFieldByFixedName("loginFailure"));
			normalFields = FieldUtil.filterDeniedFields(normalFields,deniedFields);
			long serial = 12000;
			for (Field f:normalFields){
				if (f instanceof Dropdown) {
					sl.add(new Statement(serial, 3, f.getLowerFirstFieldName() + ":parseIntNeg($(\"#ff\").find(\"#"
							+ f.getLowerFirstFieldName() + "\").combobox(\"getValue\")),"));
				} else if (f.getFieldType().equalsIgnoreCase("bool")) {
					sl.add(new Statement(serial, 3,
							f.getLowerFirstFieldName() + ":parseBoolean($(\"#ff\").find(\"input[name='"
									+ f.getLowerFirstFieldName() + "']:checked\").val()),"));
				} else if (f.getFieldType().equalsIgnoreCase("i32")||f.getFieldType().equalsIgnoreCase("i64")) {
					sl.add(new Statement(serial, 3, f.getLowerFirstFieldName() + ":parseInt($(\"#ff\").find(\"#"
							+ f.getLowerFirstFieldName() + "\").val()),"));
				} else if (f.getFieldType().equalsIgnoreCase("f64")||f.getFieldType().equalsIgnoreCase("f32")) {
					sl.add(new Statement(serial, 3, f.getLowerFirstFieldName() + ":parseFloat($(\"#ff\").find(\"#"
							+ f.getLowerFirstFieldName() + "\").val()),"));
				} else if (f.isTextarea()) {
					sl.add(new Statement(serial, 3, f.getLowerFirstFieldName() + ":$(\"#ff\").find(\"#"
							+ f.getLowerFirstFieldName() + "\").val(),"));
				} else {
					sl.add(new Statement(serial, 3, f.getLowerFirstFieldName() + ":$(\"#ff\").find(\"#"
							+ f.getLowerFirstFieldName() + "\").val(),"));
				}
				serial += 1000;
			}				
			sl.add(new Statement(serial,3, "}),"));
			sl.add(new Statement(serial+1000,3, "dataType: 'json',"));
			sl.add(new Statement(serial+2000,3, "contentType:\"application/json;charset=UTF-8\","));
			sl.add(new Statement(serial+3000,3, "success: function(data, textStatus) {"));
			sl.add(new Statement(serial+4000,4, "if (data.success) {"));
			sl.add(new Statement(serial+5000,5, "$('#ff').form('clear');"));
			sl.add(new Statement(serial+6000,5, "$(\"#ff\").find(\"input[name='"+domain.getActive().getLowerFirstFieldName()+"']\").get(0).checked = true;"));
			sl.add(new Statement(serial+7000,5, "$(\"#wadd"+domain.getCapFirstDomainName()+"\").window('close');"));
			sl.add(new Statement(serial+8000,5, "$(\"#dg\").datagrid(\"load\");"));
			if ("english".equalsIgnoreCase(this.domain.getLanguage())) {
				sl.add(new Statement(serial+8500,5, "$.messager.alert(\"Info\",\"Register user success.\",\"info\");"));
				sl.add(new Statement(serial+9000,4, "} else {"));
				sl.add(new Statement(serial+9100,5, "$.messager.alert(\"Error\",data.error,\"error\");"));
			} else {
				sl.add(new Statement(serial+8500,5, "$.messager.alert(\"信息\",\"注册用户成功！\",\"info\");"));
				sl.add(new Statement(serial+9000,4, "} else {"));
				sl.add(new Statement(serial+9100,5, "$.messager.alert(\"错误\",data.error,\"error\");"));
			}
			sl.add(new Statement(serial+9200,4, "}"));
			sl.add(new Statement(serial+10000,4, "},"));
			sl.add(new Statement(serial+11000,3, "complete : function(XMLHttpRequest, textStatus) {"));
			sl.add(new Statement(serial+12000,3, "},"));
			sl.add(new Statement(serial+13000,3, "error : function(XMLHttpRequest,textStatus,errorThrown) {"));
			sl.add(new Statement(serial+14000,4, "alert(\"Error:\"+textStatus);"));
			sl.add(new Statement(serial+15000,4, "alert(errorThrown.toString());"));
			sl.add(new Statement(serial+16000,3, "}"));
			sl.add(new Statement(serial+17000,2, "});"));
			sl.add(new Statement(serial+18000,1, "}"));
			
			method.setMethodStatementList(sl);
			return method;	
		}
	}

	@Override
	public Method generateDummyDaoImplMethod() throws Exception {
		if (this.denied) return null;
		else {
			Method method = new Method();
			method.setStandardName("Add" + StringUtil.capFirst(this.domain.getStandardName()));
			method.addSignature(new Signature(1, "&self",""));
			method.addSignature(
					new Signature(2, StringUtil.lowerFirst(this.domain.getStandardName()), new Type(this.domain.getCapFirstDomainNameWithSuffix())));
			method.setReturnType(new Type("Result<u64, Error>"));
			
			List<Writeable> sList = new ArrayList<Writeable>();
			long serial = 1000L;

			method.setMethodStatementList(WriteableUtil.merge(sList));
			return method;
		}
	}

	@Override
	public Method generateDummyServiceImplMethod() throws Exception {
		if (this.denied) return null;
		else {
			Method method = new Method();
			method.setStandardName("add" + StringUtil.capFirst(this.domain.getStandardName()));
			method.addSignature(new Signature(1, this.domain.getLowerFirstDomainName(), new Type(this.domain.getCapFirstDomainNameWithSuffix())));
			method.setReturnType(new Type("boolean"));
			method.setThrowException(true);
			method.addAdditionalImport(this.domain.getPackageToken()+".utils.PasswordUtil");

			Method daomethod = this.generateDaoMethodDefinition();
			List<Writeable> sList = new ArrayList<Writeable>();
			sList.add(NamedStatementGenerator.getDBConfInitDBAutoRelease(1000L, 2, InterVarUtil.DB.connection, InterVarUtil.DB.dbconf));
			sList.add(new Statement(1000L,3,"String "+this.domain.findFieldByFixedName("salt").getLowerFirstFieldName()+" = PasswordUtil.generateRandomSalt();"));
			sList.add(new Statement(2000L,3,"String _encPassword = PasswordUtil.multiSha1("+this.domain.getLowerFirstDomainName()+".get"+this.domain.findFieldByFixedName("password").getCapFirstFieldName()+"(), "+this.domain.findFieldByFixedName("salt").getLowerFirstFieldName()+", 3);"));
			sList.add(new Statement(3000L,3,this.domain.getLowerFirstDomainName()+".set"+this.domain.findFieldByFixedName("salt").getCapFirstFieldName()+"("+this.domain.findFieldByFixedName("salt").getLowerFirstFieldName()+");"));
			sList.add(new Statement(4000L,3,"user.set"+this.domain.findFieldByFixedName("password").getCapFirstFieldName()+"(_encPassword);"));
			sList.add(new Statement(5000L,3,"user.set"+this.domain.findFieldByFixedName("loginFailure").getCapFirstFieldName()+"(0);"));
			sList.add(new Statement(6000L,3,"return "+daomethod.generateStandardDaoImplCallString(InterVarUtil.DB.dao.getVarName())+";"));
			sList.add(NamedStatementGenerator.getLoopFooter(7000L, 2));
			method.setMethodStatementList(WriteableUtil.merge(sList));
			return method;
		}
	}

	@Override
	public Method generateDummyDaoMethodDefinition() throws Exception {
		if (this.denied) return null;
		else {
			Method method = new Method();
			method.setStandardName("add"+StringUtil.capFirst(this.domain.getStandardName()));
			method.setReturnType(new Type("boolean"));
			method.setThrowException(true);
			method.addAdditionalImport("java.util.List");
			method.addAdditionalImport(this.domain.getPackageToken()+ "."+this.domain.getDomainSuffix()+"."+this.domain.getCapFirstDomainNameWithSuffix());
			method.addSignature(new Signature(1, StringUtil.lowerFirst(this.domain.getStandardName()), this.domain.getType()));
			return method;
		}
	}
}
